# _*_ coding: UTF-8 _*_
# @Time     : 2021/11/1 18:18
# @Author   : Gong Yangyi
# @Site     : http://www.hzdledu.cn/
# @File     : abaaba.py
# @Software : PyCharm
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys  # 特殊按键类导入

class jdspider(object):
    def get_html(self):
        url = 'https://www.jd.hk/'
        driver = webdriver.Chrome()
        driver.maximize_window()
        driver.get(url)
        driver.find_element_by_xpath('//*[@id="key"]').send_keys("衣服")
        driver.find_element_by_xpath('//*[@class="form"]/button').click()
        time.sleep(5)
        driver.find_element_by_xpath('//div[@class="f-sort"]/a[2]').click()
        for i in range(1,6):
            driver.find_element_by_xpath(f'//ul[@class="gl-warp clearfix"]/li[{i}]/div[2]/div[1]/div[4]/a').click()
            time.sleep(2)
        handles = driver.window_handles
        for handle in handles:
            driver.switch_to.window(handle)
            url = driver.current_url
            if url == 'https://npcitem.jd.hk/10034786993083.html':
                break
            time.sleep(1)
        for i in range(5):
            driver.find_element_by_xpath('/html').send_keys(Keys.ARROW_DOWN)
            time.sleep(1)
        driver.find_element_by_xpath('//*[@id="choose-attr-1"]/div[2]/div[4]/a').click()
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="buy-num"]').clear()
        driver.find_element_by_xpath('//*[@id="buy-num"]').send_keys('5')
        driver.find_element_by_xpath('//*[@id="choose-attr-2"]/div[2]/div[2]/a').click()
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="InitCartUrl"]').click()
        driver.find_element_by_xpath('//*[@id="content"]/div[2]/div[1]/div/div[3]/a').click()
        driver.find_element_by_xpath('//*[@id="loginname"]').send_keys('text')
        driver.find_element_by_xpath('//*[@id="nloginpwd"]').send_keys('123456')
        driver.find_element_by_xpath('//*[@id="loginsubmit"]').click()


if __name__ == "__main__":
    spider = jdspider()
    spider.get_html()